<?php

namespace App\Http\Controllers\Admin;

use App\Exceptions\Handler;
use App\Http\Controllers\AdminBaseController;
use App\Models\AdminRoleModel;
use App\Models\HolidayModel;
use App\Services\Admin\AdminMenuService;
use App\Services\Admin\AdminRoutesService;
use App\Services\Admin\IndexService;
use App\Util\LoginUtil;
use Illuminate\Http\Request;
use JsonSchema\Uri\Retrievers\Curl;

class IndexController extends AdminBaseController
{
    protected $services = null;

    public function __construct(IndexService $Services)
    {
        parent::__construct();
        $this->services = $Services;
    }


    public function index()
    {
        $user = auth('admin')->user();
        if (!$user) {
            return redirect('/api/admin/login');
        }
        //        return view('admin.home',['user'=>$user]);
        $menuServices = app(AdminMenuService::class)->getPanelMenu();
        $str = '';
        foreach ($menuServices as $menu) {
            $str .= '<li>';
            $str .= '<a onclick="';
            $str .= empty($menu['children']) ? "xadmin.add_tab('{$menu['text']}','{$menu['href']}')" : '';
            $str .= '">';
            $str .= '<i class="layui-icon ' . $menu['icon'] . '  left-nav-li" style="font-size: 12px;" lay-tips="' . $menu['text'] . '"></i>';
            $str .= '<cite>' . $menu['text'] . '</cite>';
            $str .= !empty($menu['children']) ? "<i class=\"iconfont nav_right\">&#xe697;</i>" : "";
            $str .= '</a>';
            if (!empty($menu['children'])) {
                $str .= $this->childMenu($menu['children']);
            }
            $str .= '</li>';
        }
        $admin_info = LoginUtil::getUserInfo();
        $admin_info['role_name'] = AdminRoleModel::where('id', $admin_info['role_id'])->value('text');
        return view('admin.home', ['menu' => $str, 'name' => $admin_info['name'], 'role_name' => $admin_info['role_name']]);
    }

    private function childMenu($menu)
    {
        $str = '<ul class="sub-menu">';
        foreach ($menu as $val) {
            $str .= '<li>';
            $str .= '<a onclick="';
            $str .= empty($val['children']) ? "xadmin.add_tab('{$val['text']}','{$val['href']}')" : '';
            $str .= '">';
            // $str .= '<i class="iconfont">&#xe6a7;</i>';
            $str .= '<cite>' . $val['text'] . '</cite>';
            $str .= !empty($val['children']) ? "<i class=\"iconfont nav_right\">&#xe697;</i>" : "";
            $str .= '</a>';
            if (!empty($val['children'])) {
                $str .= $this->childMenu($val['children']);
            }
            $str .= '</li>';
        }
        $str .= '</ul>';
        return $str;
    }

    public function welcome()
    {
        return view('admin.welcome');
    }


    /** 集成后台获取菜单 */
    public function menu()
    {
        $menuServices = app(AdminMenuService::class)->getPanelMenu();
        return $menuServices;
    }




    public function captcha()
    {
        return $this->success(array([
            'status_code' => '200',
            'message' => 'created succeed',
            'url' => app('captcha')->create('default', true)
        ]));
    }

    public function showlog(Request $request)
    {
        $is_show = $request->get('is_show');
        if ($is_show) {
            $file = file_get_contents(storage_path('logs/' . $is_show));
            echo $file;
            if ($request->get('del')) {
                file_put_contents(storage_path('logs/' . $is_show), '');
            }
        }
    }

    /**代理本月新增
     * @return \Illuminate\Http\JsonResponse
     */
    public function checkToken()
    {
        if (auth('admin')->check()) {
            return $this->success();
        } else {
            return $this->failure("请重新登录", null, '', Handler::STATUS_NORMAL,  Handler::RE_CODE);
        }
    }

    public function setHoliday(Request $request)
    {
       $year = date('Y');
       $year = $request->input('year',$year);
       $list =  file_get_contents('http://timor.tech/api/holiday/year/'.$year.'/');
       $list = json_decode($list,true);
       $code = data_get($list,1);
       $holiday_list = data_get($list,'holiday',[]);
       $install_data = [];
       //删除之前的节日
       HolidayModel::query()->where('year',$year)->delete();
       if ($code==0 && !empty($holiday_list)){
           foreach ($holiday_list as $v){
               $date = data_get($v,'date','');
               $month = date('m',strtotime($date));
               $day = date('d',strtotime($date));
               $holiday = data_get($v,'holiday');

               $install_data[] = [
                   'year'=>$year,
                   'month'=>$month,
                   'day'=>$day,
                   'holiday'=>$holiday===true?1:2,
                   'name'=>data_get($v,'name',''),
                   'target'=>data_get($v,'target',''),
                   'date'=>$date,
                   'created_at'=>date('Y-m-d H:i:s'),
                   'updated_at'=>date('Y-m-d H:i:s'),
               ];
           }

           HolidayModel::query()->insert($install_data);
       }
       return true;
    }

    public function getHoliday(Request $request)
    {
        $year = date('Y');
        $year = $request->input('year',$year);
        $list = HolidayModel::query()->select(['holiday','name as text','target','date'])->where('year',$year)->get()->toArray();
        if (empty($list)) return $this->success([]);
        $data = [];
        foreach ($list as $v){
            $date = data_get($v,'date');
            $data[$date] = $v;
        }
        return $this->success($data);

    }
}
